import axios from 'axios'
import { message } from 'ant-design-vue';
import Vue from 'vue'
import contaUrl from '../../public/env.js'
const VueAxios = {
  vm: {},
  // eslint-disable-next-line no-unused-vars
  install(Vue, router = {}, instance) {
    if (this.installed) {
      return
    }
    this.installed = true

    if (!instance) {
      // eslint-disable-next-line no-console
      console.error('You have to install axios')
      return
    }

    Vue.axios = instance

    Object.defineProperties(Vue.prototype, {
      axios: {
        get: function get() {
          return instance
        },
      },
      $http: {
        get: function get() {
          return instance
        },
      },
    })
  },
}
/**
 * 【指定 axios的 baseURL】
 * 如果手工指定 baseURL: '/jeecg-boot'
 * 则映射后端域名，通过 vue.config.js
 * @type {*|string}
 */
const apiBaseUrl = contaUrl.baseUrl || '/cloud_job'
// console.log("apiBaseUrl= ",apiBaseUrl)
// 创建 axios 实例
const Axios = axios.create({
  // baseURL: '/jeecg-boot',
  baseURL: apiBaseUrl, // api base_url
  timeout: 9000, // 请求超时时间
})

// request interceptor
Axios.interceptors.request.use(
  (config) => {
    const token = sessionStorage.getItem('accesstoken')
    if (token) {
      config.headers['X-Access-Token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
    }
    // update-begin-author:taoyan date:2020707 for:多租户
    let tenantid = sessionStorage.getItem('TENANT_ID')
    if (!tenantid) {
      tenantid = 0
    }
    config.headers['tenant_id'] = tenantid
    // update-end-author:taoyan date:2020707 for:多租户
    if (config.method == 'get') {
      if (config.url.indexOf('sys/dict/getDictItems') < 0) {
        config.params = {
          _t: Date.parse(new Date()) / 1000,
          ...config.params,
        }
      }
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

Axios.interceptors.response.use(function(response){
  setTimeout(()=>{
      // endLoading();
      // console.log('????/')
  },300);

  if(response){
      switch (response.data.code) {
          case 0:
              break;

          case 3000002:
            sessionStorage.clear()
            router.push('/login');
            break;    
          case 3000003:
              // console.Console('???')
              axios({
                  method: 'get',
                  url:contaUrl.baseUrl+'/code_demo/user/token',
                  headers: {
                      refreshToken:sessionStorage.getItem('refreshToken')
                  },
              }).then((res) => {
                  if(res.data.code ==0){
                      // this.$message.info('凭证更新成功');
                      sessionStorage.removeItem('accessToken')
                      sessionStorage.setItem('accessToken',res.data.data)
                      let NewPage = '_empty' + '?time=' + new Date().getTime() / 500;
                      router.push(NewPage);
                      router.go(-1);
                      
                  }else {
                  }
              }).catch((error) => {
                  console.log(error)
              })
              break;
              case 4000010:
              // console.Console('???')
              sessionStorage.clear()
              router.push('/login');
              break;  
          default:

              // axios({
              //     method: 'get',
              //     url:'https://www.inspectss.com/api/user/token',
              //     headers: {
              //         accesstoken:localStorage.getItem('accesstoken'),
              //         refreshtoken:localStorage.getItem('refreshtoken')
              //     },
              // }).then((res) => {
              //     if(response.data.code ==0){
              //         localStorage.removeItem('accesstoken')
              //         localStorage.setItem('accesstoken',response.data.data)
              //
              //     }else {
              //     }
              // }).catch((error) => {
              //     console.log(error)
              // })
              // this.$message.error(response.data.msg);
              console.log(response.data.code)
              break;
      }
  }
  return response
},function(error){
  router.push('/login');
  return Promise.reject(error)
});
// post
export function postAction(url, parameter) {
  return Axios({
    url: url,
    method: 'post',
    data: parameter,
  })
}

// post method= {post | put}
export function httpAction(url, parameter, method) {
  return Axios({
    url: url,
    method: method,
    data: parameter,
  })
}

// put
export function putAction(url, parameter) {
  return Axios({
    url: url,
    method: 'put',
    data: parameter,
  })
}

// get
export function getAction(url, parameter) {
  return Axios({
    url: url,
    method: 'get',
    params: parameter,
  })
}

// deleteAction
export function deleteAction(url, parameter) {
  return Axios({
    url: url,
    method: 'delete',
    params: parameter,
  })
}
export { VueAxios }
